<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%;"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleClick">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="edit">编辑部分</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/departments'
export default {
  props: {
    // 定义传入的属性
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    // 删除，查看，编辑的点击事件 str就是command的值
    async handleClick(str) {
      // 添加
      if (str === 'add') {
        // 修改添加部门对话框的状态
        this.$emit('updateState', this.nodeData)
      }
      // 编辑
      if (str === 'edit') {
        // 通知父组件,将对话框展开
        this.$emit('editDept', this.nodeData)
      }
      // 删除
      if (str === 'del') {
        const res = await this.$confirm('是否删除此部门?', '提示', {
          type: 'warning'
        }).catch(() => {
          this.$message.info('已取消操作')
        })
        if (res === 'confirm') {
          try {
            await reqDelDepartments(this.nodeData.id)
            // 通知父组件重新渲染树形结构
            this.$emit('toRender')
          } catch (error) {
            this.$message.error('操作失败,请稍后重试')
          }
        }
      }
    }
  }
}
</script>
